package com.cpi.cornpp.transports.action;

import java.util.Iterator;
import java.util.List;
import java.util.Locale;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.commons.collections.CollectionUtils;
import org.apache.commons.lang.StringUtils;
import org.apache.struts.Globals;
import org.apache.struts.action.Action;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.util.MessageResources;

import com.cpi.cornpp.corntypes.view.CornTypesView;
import com.cpi.cornpp.transports.exception.TransportException;
import com.cpi.cornpp.transports.form.TransportForm;
import com.cpi.cornpp.transports.service.TransportService;
import com.cpi.cornpp.transports.view.CostElementView;
import com.cpi.cornpp.transports.view.TransportChargeView;
import com.cpi.cornpp.transports.view.TransportView;

public class SaveEditTransportAction extends Action {
	private static final String SUCCESS_FORWARD = "toSuccess";
	private static final String NEW_TRANSPORT = "addNewTransport";
	private static final String FAILURE_FORWARD = "error";
	/** Information message name*/
	private final static String MESSAGE = "MESSAGE";//$NON-NLS-1$
	/** Success add key */
	private static final String SUCCESS_EDIT_LABEL = "common.edit.success";//$NON-NLS-1$
	
	public ActionForward execute(ActionMapping mapping, ActionForm form, HttpServletRequest request, HttpServletResponse response) throws TransportException{
		TransportForm transportForm = null;
		TransportView transportView = null;
		CornTypesView cornTypesView = null;
		TransportChargeView transportChargeView = null;
		CostElementView costElementView = null;
		TransportService transportService = null;
		List deletedCostElements = null;
		List addedCostElements = null;
		HttpSession session = null;
		ActionForward forward = null;
		String messageError = null;
		MessageResources messageResources = null;
		String addNewTransport = null;
		
		forward = mapping.findForward(FAILURE_FORWARD);
		session = request.getSession(false);
		
		deletedCostElements = (List) session.getAttribute("deletedTransportCostElements");
		addedCostElements = (List) session.getAttribute("addedTransportCostElements");
		
		try{
			
			if (form != null){
				messageResources = getResources(request);
				transportForm = (TransportForm) form;
				
				addNewTransport = transportForm.getCreateNewTransport();

				transportView = new TransportView();
				cornTypesView = new CornTypesView();
				transportService = new TransportService();
				
				transportView.setTransportIdentifier(Integer.parseInt(transportForm.getId()));
				transportView.setTransportShortName(transportForm.getCode());
				transportView.setTransportDescription(transportForm.getDescription());
				transportView.setTransportCapacity(StringUtils.isNotEmpty(transportForm.getCapacity()) ? Double.parseDouble(transportForm.getCapacity()) : 0.0D);
				transportView.setTransportPriority(StringUtils.isNotEmpty(transportForm.getPriority()) ? Integer.parseInt(transportForm.getPriority()) : 0);
				transportView.setTransportNomenclature(transportForm.getNomenclature());
				cornTypesView.setCornTypeId(transportForm.getCornTypeId());
				transportView.setCornTypesView(cornTypesView);
				
				transportService.updateTransport(transportView);
				
				if (CollectionUtils.isNotEmpty(deletedCostElements)){
					
					for (Iterator itr = deletedCostElements.iterator(); itr.hasNext(); ){
						transportChargeView = new TransportChargeView();
						costElementView = (CostElementView) itr.next();
						
						transportChargeView.setTransportChargeTransportId(transportView.getTransportIdentifier());
						transportChargeView.setTransportChargeTypeId(costElementView.getChargeTypeId());
						
						transportService.removeTransportCharge(transportChargeView);
					}
				}
				
				if (CollectionUtils.isNotEmpty(addedCostElements)){
					
					for (Iterator itr = addedCostElements.iterator(); itr.hasNext(); ){
						transportChargeView = new TransportChargeView();
						costElementView = (CostElementView) itr.next();
						
						transportChargeView.setTransportChargeTransportId(transportView.getTransportIdentifier());
						transportChargeView.setTransportChargeTypeId(costElementView.getChargeTypeId());
						transportChargeView.setTransportChargeAmount(costElementView.getChargeTypeCost());
						
						transportService.addTransportCharge(transportChargeView);
					}
				}
				
				messageError = messageResources.getMessage((Locale)session.getAttribute(Globals.LOCALE_KEY), SUCCESS_EDIT_LABEL);
				session.setAttribute(MESSAGE, messageError);
				
				forward = ("1".equalsIgnoreCase(addNewTransport) ? mapping.findForward(NEW_TRANSPORT) : mapping.findForward(SUCCESS_FORWARD));
			}
			
		} catch(TransportException tre){
			
		} catch(Exception e){
			
		}
		
		return forward;
	}
}